// Created by Joe on 2017/6/14.

app.service(
    'MockService',
    [
        '$q', '$timeout',
        function ($q, $timeout) {
            this.userId = 1;
            this.userList = [{
                id: 0,
                phone: '18253189750',
                password: '1234',
                name: 'Joe',
                card: '340123199401314115',
                certification: true
            }];
            var that = this;
            return {
                getSocialInsuranceMenu: function () {
                    var deferred = $q.defer();
                    var menuData = [
                        {name: '养老金个人账户查询', value: '1682.03'},
                        {name: '医疗个人账户查询', value: '1682.03'},
                        {name: '离退休个人养老金查询', value: '暂无数据'},
                        {name: '失业人员享受待遇查询', value: '暂无数据'}
                    ];
                    $timeout(function () {
                        deferred.resolve(menuData);
                    }, 100);
                    return deferred.promise;
                },
                getPensionDetail: function () {
                    var deferred = $q.defer();
                    var pensionData = [
                        {name: '单位名称', val: '浪潮集团'},
                        {name: '年度', val: '2015'},
                        {name: '累计缴费月度', val: '12'},
                        {name: '单位累计', val: '20,582.08'},
                        {name: '个人累计', val: '582.08'},
                        {name: '当年缴费月度', val: '8'},
                        {name: '当年单位划入', val: '125,582.08'},
                        {name: '当年单位利息', val: '82.08'},
                        {name: '当年单位总入', val: '900,980.10'},
                        {name: '备注', val: '暂无'}
                    ];
                    $timeout(function () {
                        deferred.resolve(pensionData);
                    });
                    return deferred.promise;
                },
                getMedicalTreatmentDetail: function (start, end) {
                    var deferred = $q.defer();
                    var startDate = start.split('-');
                    var endDate = end.split('-');

                    var medicalTreatmentData = [
                        {time: '2016-07-01 12:00:01', data: '11.20', name: '利息收入'},
                        {time: '2016-08-01 09:08:19', data: '20.61', name: '财产收入'},
                        {time: '2016-09-01 13:43:56', data: '442.32', name: '资金总额'},
                        {time: '2016-10-01 19:08:33', data: '32.93', name: '日常补助'},
                        {time: '2016-11-01 01:01:15', data: '33.00', name: '额外补助'},
                        {time: '2016-12-01 00:00:00', data: '98.21', name: '补助总额'},
                        {time: '2017-01-01 12:12:12', data: '115.98', name: '救济资金'},
                        {time: '2017-02-01 08:19:07', data: '200.09', name: '救济总额'},
                        {time: '2017-03-01 20:10:19', data: '40.92', name: '日常收入'},
                        {time: '2017-04-01 23:56:18', data: '96.64', name: '月度收入'},
                        {time: '2017-05-01 07:08:19', data: '96.65', name: '额外收入'},
                        {time: '2017-06-01 19:10:34', data: '96.61', name: '收入总额'}
                    ];

                    var dateCompare = function (d1, d2) {
                        // return d1.every(function(item, index) {
                        //     return parseInt(item, 10) >= parseInt(d2[index], 10);
                        // });
                        for (var i = 0; i < d1.length; i++) {
                            if (parseInt(d1[i], 10) > parseInt(d2[i], 10)) {
                                return true;
                            } else if (parseInt(d1[i], 10) < parseInt(d2[i], 10)) {
                                return false;
                            }
                        }
                        return true;
                    };

                    $timeout(function () {
                        deferred.resolve(medicalTreatmentData.filter(function (item) {
                            var date = item.time.split('-');
                            return dateCompare(date, startDate) && dateCompare(endDate, date);
                        }));
                    });

                    return deferred.promise;
                },
                registerUser: function (phone, password) {
                    var deferred = $q.defer();
                    $timeout(function () {
                        var user = {
                            id: ++that.userId,
                            phone: phone,
                            password: password,
                            name: null,
                            card: null,
                            certification: false
                        };
                        that.userList.push(user);
                        deferred.resolve(user);
                    }, 100);
                    return deferred.promise;
                },
                getUserByPassword: function (phone, password) {
                    var deferred = $q.defer();
                    $timeout(function () {
                        deferred.resolve(that.userList.filter(function (item) {
                            return item.phone === phone && item.password === password;
                        }));
                    }, 100);
                    return deferred.promise;
                },
                getUserByVerificationCode: function (phone) {
                    var deferred = $q.defer();
                    $timeout(function () {
                        deferred.resolve(that.userList.filter(function (item) {
                            return item.phone === phone;
                        }));
                    }, 100);
                    return deferred.promise;
                },
                userCertification: function (phone, name, card) {
                    var deferred = $q.defer();
                    $timeout(function () {
                        for (var i = 0; i < that.userList.length; i++) {
                            if (that.userList[i].phone === phone) {
                                that.userList[i].name = name;
                                that.userList[i].card = card;
                                that.userList[i].certification = true;
                                deferred.resolve(true);
                                break;
                            }
                        }
                        deferred.resolve(false);
                    }, 100);
                    return deferred.promise;
                }
            };
        }
    ]
);